package ${packageName};

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import ${basePackageName}entity.base.BoPage;
import ${basePackageName}entity.base.JsonResult;
import ${basePackageName}entity.${className};
import ${basePackageName}entity.bean.${className}Req;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author： XW -> ~
 * @create： 2024/5/29 14:06
 */
@Slf4j
public class ${className}ControllerTest {

    // 数据库中的数据量
    private int dataSize = 10;

    private int createSize = 5;

    @Test
    public void allTest() {

        // 创建
        createTest();

        // 修改
        putTest();

        // 删除
        deleteTest();

    }

    @Test
    public void deleteTest() {
        ${className}Req ${className?uncap_first}Req = new ${className}Req();
        ${className?uncap_first}Req.setPageNumber(1);
        ${className?uncap_first}Req.setPageSize(createSize);
        ${className?uncap_first}Req.set${fieldTime?cap_first}(1);

        JsonResult<BoPage<${className}>> jsonResult = getResult${className}(${className?uncap_first}Req);
        printResult(jsonResult);

        if (jsonResult == null || jsonResult.getData() == null || CollectionUtils.isEmpty(jsonResult.getData().getList())) {
            return;
        }

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8414/vv/${className?uncap_first}/";

        List<${className}> messagesList = jsonResult.getData().getList();
        for (${className} ${className?uncap_first} : messagesList) {
            String getUrl = url+${className?uncap_first}.getId();

            // 取一下详情接口
            String forObject = restTemplate.getForObject(getUrl, String.class);
            JsonResult<${className}> parseResult = JSON.parseObject(forObject, new TypeReference<JsonResult<${className}>>() {
            });
            if (null == parseResult || parseResult.getData() == null){
                log.error("查询详情接口，可能有问题");
            }

            String deleteUrl = url+"/delete/"+${className?uncap_first}.getId();

            // 删除数据
            restTemplate.getForObject(deleteUrl,String.class);
        }

        JsonResult<BoPage<${className}>> againResult = getResult${className}(${className?uncap_first}Req);
        printResult(jsonResult);

        if (againResult == null || againResult.getData() == null) {
            log.error("应该的，但是感觉有点问题");
            return;
        }

        if (!CollectionUtils.isEmpty(againResult.getData().getList())) {
            log.error("删除操作，应该有点问题");
            return;
        }
        log.error("删除干净了");
    }

    @Test
    public void putTest() {

        ${className}Req ${className?uncap_first}Req = new ${className}Req();
        ${className?uncap_first}Req.setPageNumber(1);
        ${className?uncap_first}Req.setPageSize(createSize);
        ${className?uncap_first}Req.set${fieldTime?cap_first}(1);

        JsonResult<BoPage<${className}>> jsonResult = getResult${className}(${className?uncap_first}Req);
        printResult(jsonResult);

        if (jsonResult == null || jsonResult.getData() == null || CollectionUtils.isEmpty(jsonResult.getData().getList())) {
            return;
        }

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8414/vv/${className?uncap_first}/put";

        List<${className}> messagesList = jsonResult.getData().getList();
        for (${className} ${className?uncap_first} : messagesList) {
            String updateUrl = url+${className?uncap_first}.getId();
            ${className?uncap_first}.setCreatedAt(2);

            restTemplate.postForObject(updateUrl,${className?uncap_first},String.class);
        }

        ${className?uncap_first}Req.setCreatedAt(2);
        JsonResult<BoPage<${className}>> updateResult = getResult${className}(${className?uncap_first}Req);
        printResult(jsonResult);

        if (updateResult == null || updateResult.getData() == null) {
            log.info("修改可能出现问题");
            return;
        }
        int size = jsonResult.getData().getList().size();
        int updateSize = updateResult.getData().getList().size();

        if (size != updateSize){
            log.info("修改可能出现问题,数量上对不上,size={},uSize={}",size,updateSize);
            return;
        }
        log.info("完美！！！");

        List<${className}> updateList = updateResult.getData().getList();
        for (${className} ${className?uncap_first} : updateList) {
            String updateUrl = url+${className?uncap_first}.getId();
            ${className?uncap_first}.setCreatedAt(1);

            restTemplate.put(updateUrl,${className?uncap_first});
        }

        ${className?uncap_first}Req.setCreatedAt(2);
        JsonResult<BoPage<${className}>> backResult = getResult${className}(${className?uncap_first}Req);
        printResult(backResult);

        log.info("修改完了，再变回去呀，完美！！！");
    }


    @Test
    public void createTest() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8414/vv/${className?uncap_first}";

        ${className} ${className?uncap_first} = new ${className}();
<#list fields as field>
    <#if field.name == "id">
    <#else >
        <#if field.type == "String">
        ${className?uncap_first}.set${field.name?cap_first}("1");
        <#elseif field.type == "Byte">
        ${className?uncap_first}.set${field.name?cap_first}((byte)1);
        <#elseif field.type == "Long">
        ${className?uncap_first}.set${field.name?cap_first}(1L);
        <#else>
        ${className?uncap_first}.set${field.name?cap_first}(1);
        </#if>
    </#if>
</#list>

        for (int i = 0; i < createSize; i++) {
            String pub = restTemplate.postForObject(url, JSON.toJSON(${className?uncap_first}), String.class);
            log.info("pub = {}", pub);
        }
    }

    @Test
    public void simpleControlPageTest() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8414/vv/${className?uncap_first}/page/1/1";
        String pub = restTemplate.getForObject(url, String.class);
        JsonResult<BoPage<${className}>> jsonResult = JSON.parseObject(pub, new TypeReference<JsonResult<BoPage<${className}>>>() {
        });

        printResult(jsonResult);
    }

    @Test
    public void reqControlPageTest() {

        ${className}Req ${className?uncap_first}Req = new ${className}Req();
        ${className?uncap_first}Req.setPageNumber(1);
        ${className?uncap_first}Req.setPageSize(1);

        JsonResult<BoPage<${className}>> jsonResult = getResult${className}(${className?uncap_first}Req);

        printResult(jsonResult);
    }

    private JsonResult<BoPage<${className}>> getResult${className}(${className}Req ${className?uncap_first}Req) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8414/vv/${className?uncap_first}/pageReq";

        String pub = restTemplate.postForObject(url, JSON.toJSON(${className?uncap_first}Req), String.class);
        return JSON.parseObject(pub, new TypeReference<JsonResult<BoPage<${className}>>>() {
        });
    }

    private void printResult(JsonResult<BoPage<${className}>> jsonResult) {
        if (jsonResult == null) {
            log.error("返回空的");
        }
        if (jsonResult.getData() == null) {
            log.error("BoPage是空的");
        }
        if (jsonResult.getData().getTotal() == 0 && dataSize != 0) {
            log.error("返回结果显示数据库中没有数据！！！");
        }
        log.info("boPage.size = {}", jsonResult.getData().getTotal());
    }

}
